var express = require('express');
var router = express.Router();
var uuid = require('uuid').v1;




// Projects

router.get('/project/deteil', function (req, res, next) {
    var id = req.query.id;
    query(`SELECT * FROM project WHERE disable = 0 AND project_id = ?`, [id], function (qerr, vals, fields) {
        if (qerr) {
            res.status(500);
            res.json({
                msg: qerr
            });
        } else {
            if (vals.length == 1) {
                res.json(vals[0]);
            } else {
                res.status(500);
                res.json({
                    msg: '未找到项目'
                });
            };
        };
    });
});

router.get('/project/status', function (req, res, next) {
    var id = req.query.id;
    query(`select status, count(status) AS num FROM project_api AS a WHERE project_id = ? AND disable = 0 GROUP BY status`, [id], function (qerr, vals, fields) {
        if (qerr) {
            res.status(500);
            res.json({
                msg: qerr
            });
        } else {
            res.json(vals);
        };
    });
});




router.get('/project', function (req, res, next) {
    query(`SELECT * FROM project WHERE disable = 0`, [], function (qerr, vals, fields) {
        if (qerr) {
            res.status(500);
            res.json({
                msg: qerr
            });
        } else {
            res.json(vals);
        };
    });
});


router.post('/project', async function (req, res, next) {
    var projectId = uuid();
    var userId = req.headers.token;
    var projectSql = `INSERT INTO project SET ?`;
    var folderSql = `INSERT INTO folder SET ?`;
    var projectUserSql = `INSERT INTO project_user SET ?`;
    try {
        await asyncQuery(projectSql, {
            project_id: projectId,
            title: req.body.title,
            description: req.body.description,
            disable: 0,
            owner_user: userId,
        });
        await asyncQuery(folderSql, {
            folder_id: uuid(),
            folder_project_id: projectId,
            title: '根目录',
            type: 0,
            description: '根目录',
            disable: 0
        });
        await asyncQuery(projectUserSql, {
            project_user_id: uuid(),
            project_id: projectId,
            user_id: userId,
            permission: 'admin',
        });
        res.json({
            id: projectId,
        });
    } catch (error) {
        next(error)
    };
});

router.put('/project', async function (req, res, next) {
    var userId = req.headers.token;
    var projectId = req.body.id;
    try {
        var value = await Permission.CPP(userId, projectId);
        if (value == 'admin') {
            var projectSql = `UPDATE project SET ? WHERE project_id = ?`;
            await asyncQuery(projectSql, [{ title: req.body.title, description: req.body.description, }, projectId,]);
            res.json({});
        }
    } catch (error) {
        next(error)
    };
});


router.delete('/project', async function (req, res, next) {
    var userId = req.headers.token;
    var projectId = req.body.id;
    try {
        var value = await Permission.CPP(userId, projectId);
        if (value == 'admin') {
            var projectSql = `UPDATE project SET ? WHERE project_id = ?`;
            await asyncQuery(projectSql, [{ disable: 1 }, req.body.id,]);
            res.json({});
        }
    } catch (error) {
        next(error)
    };
});


router.get('/project/userProject', async function (req, res, next) {
    var userId = req.headers.token;
    var Sql = `SELECT p.*, u.username, u.nickname 
    FROM project p, \`user\` u, project_user pu 
    WHERE pu.user_id = ?
    AND u.user_id = p.owner_user 
    AND pu.project_id = p.project_id
    AND p.\`disable\` = 0`;
    try {
        var vals = await asyncQuery(Sql, [userId]);
        res.json(vals);
    } catch (error) {
        next(error)
    };
});


router.get('/project/userList', async function (req, res, next) {
    var projectId = req.query.projectId;
    var Sql = `SELECT pu.*, u.username, u.nickname , u.avatar, u.gender 
    FROM \`user\` u, project_user pu 
    WHERE pu.project_id = ?
    AND pu.user_id = u.user_id`;
    try {
        var vals = await asyncQuery(Sql, [projectId]);
        res.json(vals);
    } catch (error) {
        next(error)
    };
})

router.put('/project/Permission', async function (req, res, next) {
    var projectId = req.query.projectId;
    var userId = req.headers.token;
    try {
        var value = await Permission.CPP(userId, projectId);
        if (value == 'admin') {
            var projectSql = `UPDATE project_user SET ? WHERE project_user_id = ?`;
            await asyncQuery(projectSql, [{
                permission: req.body.permission,
            }, req.body.id]);
            res.json({});
        }
    } catch (error) {
        next(error)
    };
});

router.delete('/project/user', async function (req, res, next) {
    var projectId = req.query.projectId;
    var userId = req.headers.token;
    try {
        var value = await Permission.CPP(userId, projectId);
        if (value == 'admin') {
            var projectSql = `DELETE FROM project_user WHERE project_user_id = ?`;
            await asyncQuery(projectSql, [req.body.id]);
            res.json({});
        }
    } catch (error) {
        next(error)
    };
});



module.exports = router;